package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.GoodsCategoryLog;
import com.dingreading.cloud.admin.entity.table.GoodsCategoryLogTableDef;
import com.dingreading.cloud.admin.mapper.GoodsCategoryLogMapper;
import com.dingreading.cloud.admin.service.GoodsCategoryLogService;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-02-21
 */
@Service
public class GoodsCategoryLogServiceImpl extends BaseServiceImpl<GoodsCategoryLogMapper, GoodsCategoryLog, GoodsCategoryLogTableDef> implements GoodsCategoryLogService {


    @Override
    protected GoodsCategoryLogTableDef getTable() {
        return GoodsCategoryLogTableDef.goodsCategoryLog;
    }

    @Override
    public int getMaxOrderNum(Long categoryId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .where(table.categoryId.eq(categoryId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public List<GoodsCategoryLog> getByGoodsId(Long goodsId) {
        return getMapper().selectListByCondition(table.goodsId.eq(goodsId));
    }

    @Override
    public void dropByGoodsIdAndIdsNot(Long goodsId, List<Long> ids) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.goodsId.eq(goodsId));
        if (CollectionUtils.isNotEmpty(ids))
            condition.and(table.id.notIn(ids));
        getMapper().deleteByCondition(condition);
    }

    @Override
    public GoodsCategoryLog getGtShowOrder(Long categoryId, Integer showOrder) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.categoryId.eq(categoryId))
                .and(table.orderNum.gt(showOrder));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public GoodsCategoryLog getLtShowOrder(Long categoryId, Integer showOrder) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.categoryId.eq(categoryId))
                .and(table.orderNum.lt(showOrder));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public GoodsCategoryLog getTopOrder(Long categoryId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.min(table.orderNum))
                .from(table)
                .where(table.categoryId.eq(categoryId));

        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.categoryId.eq(categoryId))
                .and(table.orderNum.in(wrapper));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public void updateShowOrder(Long id, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id));
        UpdateChain.of(GoodsCategoryLog.class)
                .set(table.orderNum, orderNum)
                .where(condition)
                .update();
    }

    @Override
    public GoodsCategoryLog getByGoodsIdAndCategoryId(Long goodsId, Long categoryId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.goodsId.eq(goodsId))
                .and(table.categoryId.eq(categoryId));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public long getByCategoryId(Long categoryId) {
        return getMapper().selectCountByCondition(table.categoryId.eq(categoryId));
    }

    @Override
    public void updateOtherOrderAddOne(Long id, Long categoryId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.ne(id))
                .and(table.categoryId.eq(categoryId));

        UpdateChain.of(GoodsCategoryLog.class)
                .set(table.orderNum, table.orderNum.add(1))
                .where(condition)
                .update();
    }

}
